package com.max.eshop.parsers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.max.eshop.core.Order;

public class ParserOrder {
	private String[] ordersFile;
	private ArrayList<Order> orders = new ArrayList<Order>();
	private final static String space = " ";

	public ArrayList<Order> returnOrders(String[] ordersFile) {
		for (int i = 0; i < ordersFile.length - 1; i++) {
			if (!ordersFile[i + 1].equals(null)) {
				String temp[] = ordersFile[i + 1].split(" ");
				Date date = null;
				try {
					date = new SimpleDateFormat("dd.MM.yyyy").parse(temp[0]);

				} catch (ParseException e) {
					e.printStackTrace();
				}
				try {
					orders.add(new Order(date, Boolean.parseBoolean(temp[1]),
							Integer.parseInt(temp[2]), temp[3], Integer
									.parseInt(temp[4])));
				} catch (NullPointerException e) {
					System.out.println("ERROR");
				}
			}

		}
		return orders;
	}

	public String[] booksToString(List<Order> orders2) {
		ordersFile = new String[orders2.size() + 1];
		ordersFile[0] = "date status price nameBook count";
		DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		for (int i = 0; i <= orders2.size() - 1; i++) {
			ordersFile[i + 1] = dateFormat.format(orders2.get(i).getDate())
					+ space + orders2.get(i).getStatus() + space
					+ orders2.get(i).getPrice() + space
					+ orders2.get(i).getNameBook() + space
					+ orders2.get(i).getCount();
		}
		return ordersFile;
	}

}
